package com.boxin.ims.modules.wechat.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.tools.JavaFileManager.Location;

import net.sf.cglib.core.Local;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.entity.CouponConfig;
import com.boxin.ims.modules.momarketing.service.CouponConfigService;
import com.boxin.ims.modules.wechat.entity.MemberMoney;
import com.boxin.ims.modules.wechat.entity.MemberScore;
import com.boxin.ims.modules.wechat.entity.ScoreRule;
import com.boxin.ims.modules.wechat.entity.SignupConfig;
import com.boxin.ims.modules.wechat.entity.UserDefine;
import com.boxin.ims.modules.wechat.entity.WeChat;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.entity.tmessage.BusinessMessageTemplate;
import com.boxin.ims.modules.wechat.entity.tmessage.UserMessageTemplate;
import com.boxin.ims.modules.wechat.score.entity.ExchangeRecord;
import com.boxin.ims.modules.wechat.score.entity.ScoreExchange;
import com.boxin.ims.modules.wechat.score.service.ExchangeRecordService;
import com.boxin.ims.modules.wechat.service.MemberMoneyService;
import com.boxin.ims.modules.wechat.service.MemberScoreService;
import com.boxin.ims.modules.wechat.service.SignupConfigService;
import com.boxin.ims.modules.wechat.service.WeChatService;
import com.boxin.ims.modules.wechat.service.WechatMemberService;
import com.boxin.ims.modules.wechat.service.tmessage.UserMessageTemplateService;
import com.boxin.ims.modules.wechat.tmessage.TemplateMessage;
import com.boxin.ims.modules.wechat.tmessage.TemplateMessageFactory;
import com.thinkgem.jeesite.modules.sys.entity.User;

@Service
public class ScoreUtils   implements ApplicationContextAware {
	
	public static Integer DEFAULT_ADD_SCORE = 1;
	
	private static MemberScoreService memberScoreService;
	
	private static WechatMemberService wechatMemberService;
	
	private static CouponConfigService couponConfigService;
	
	private static ExchangeRecordService exchangeRecordService;
	
	private static MemberMoneyService	memberMoneyService;
	
	private static UserMessageTemplateService userMessageTemplateService;
	
	private static WeChatService weChatService;
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-9  上午11:49:38
	 * @function <p>微信签到积分 </p>
	 * @param wechatMember
	 */
	public static void addWechatMemberSignScore(WechatMember wechatMember,int score ){
		
		MemberScore memberScore = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(score);			//操作的分数，有可能是负数 
		memberScore.setType("0");
		memberScore.setRemarks("微信签到"+score/1000d+"个活动积分");
		memberScoreService.save(memberScore);
	}
	
	/**
	 * 三级推广积分添加
	 * @param wechatMember
	 * @param recommendScore
	 * @param reason
	 * @param type
	 */
	public static void addRecommendScore(WechatMember wechatMember,int recommendScore,String reason,String type ){
		
		wechatMemberService.addRecommendScoreById(recommendScore,wechatMember.getId());
		
		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setType(type);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(recommendScore);			//操作的分数，有可能是负数 
		memberScore.setRemarks(reason);
		memberScoreService.save(memberScore);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-24  下午3:08:33
	 * @function <p> 添加或扣除积分 </p>
	 * @param wechatMember
	 * @param score
	 * @param reason
	 */
	public static void addScore(WechatMember wechatMember,int score,String type,String remarks){
		
		MemberScore memberScore  = new MemberScore();
		
				//活动积分
				if("0".equals(type)){
					wechatMember.setActiveScore(score+((wechatMember.getActiveScore()== null)?0:wechatMember.getActiveScore()));
					wechatMember.setScore(score+wechatMember.getScore());
					memberScore.setType(type);
				}
				//消费积分
				else if("1".equals(type)){
					wechatMember.setConsumeScore(score+((wechatMember.getConsumeScore()== null)?0:wechatMember.getConsumeScore()));
					wechatMember.setScore(score+wechatMember.getScore());
					memberScore.setType(type);
				}
				//充值积分
				else if("2".equals(type)){
					wechatMember.setCreditScore(score+((wechatMember.getCreditScore()== null)?0:wechatMember.getCreditScore()));
					wechatMember.setScore(score+wechatMember.getScore());
					memberScore.setExtType("1");//可用积分
					memberScore.setType(type);
				}
				//充值押金
				else if("3".equals(type)){
					wechatMember.setUnusable(score+((wechatMember.getUnusable()== null)?0:wechatMember.getUnusable()));
					wechatMember.setScore(score+wechatMember.getScore());
					memberScore.setExtType("0");//不可用积分
					memberScore.setType(type);
				}
				//消费积分
				else if("4".equals(type)){
					wechatMember.setConsumeScore(score+((wechatMember.getConsumeScore()== null)?0:wechatMember.getConsumeScore()));
					wechatMember.setScore(score+wechatMember.getScore());
					memberScore.setType("1");
				}
				
				memberScore.setWechatMember(wechatMember);
				memberScore.setUser(wechatMember.getUser());
				memberScore.setScore(score);	//操作的分数，有可能是负数 	
				memberScore.setRemarks(remarks);
				wechatMemberService.save(wechatMember);
				memberScoreService.save(memberScore);
	}


	/**
	 * @author Jakemanse
	 * @time 2013-10-24  下午3:08:33
	 * @function <p> 添加或扣除积分 </p>
	 * @param wechatMember
	 * @param score
	 * @param reason
	 */
	public static void addScore(WechatMember wechatMember,int score,String reason ){
		
		int resultScore = wechatMember.getScore() + score;
		wechatMember.setScore((resultScore<0)?0:resultScore);
		wechatMemberService.save(wechatMember);

		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(score);			//操作的分数，有可能是负数 
		memberScore.setRemarks(reason);
		memberScoreService.save(memberScore);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-5-7  下午1:16:27
	 * @function <p> 增加奖金 </p>
	 * @param wechatMember
	 * @param money
	 * @param reason
	 */
	public static void addMoney(WechatMember wechatMember,int money,String reason ){
		
		int resultMoney = wechatMember.getMoney() + money;
		wechatMember.setMoney((resultMoney<0)?0:resultMoney);
		wechatMemberService.save(wechatMember);
		
		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(money);			//操作的分数，有可能是负数 
		memberScore.setRemarks(reason);
		memberScoreService.save(memberScore);
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-25  上午8:57:10
	 * @function <p>会员充值  </p>
	 * @param wechatMember
	 * @param score
	 */
	public static void addWechatMemberChargeScore(WechatMember wechatMember,int score,String type,String remarks){
		
		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setType(type);
		memberScore.setScore(score);			//操作的分数，有可能是负数 
		memberScore.setRemarks(remarks);
		if("2".equals(type)){
			memberScore.setExtType("0");//不可用积分
		}
		memberScoreService.save(memberScore);
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-25  上午8:57:10
	 * @function <p>取消关注，积分清零  </p>
	 * @param wechatMember
	 * @param score
	 */
	public static void addWechatCancelScore(WechatMember wechatMember,int score ){
		
		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(-score);			//操作的分数，有可能是负数 
		memberScore.setRemarks("取消关注后，扣除"+((double)score/1000)+"积分");
		memberScoreService.save(memberScore);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-1-20  上午12:25:47
	 * @function <p> 积分兑换 </p>
	 * @param wechatMember
	 * @param score
	 * @param remarks
	 */
	public static void addWechatExchangeScore(WechatMember wechatMember,int score,String remarks ){
		
		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(-score);			//操作的分数，有可能是负数 
		memberScore.setRemarks(remarks);
		memberScoreService.save(memberScore);
	}
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-24  下午2:19:25
	 * @function <p> 积分兑换，消费 </p>
	 * @param wechatMember
	 * @param score
	 */
	public static ExchangeRecord  addWechatMemberConsumeScore(WechatMember wechatMember, ScoreExchange exchange ){
		
		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(-exchange.getScore());			//操作的分数， 是负数 
		memberScore.setRemarks("微信积分兑换" + exchange.getName() +",消费了"+((double)exchange.getScore()/1000) +"积分.");
		memberScoreService.save(memberScore);
		//添加积分兑换记录
		ExchangeRecord record = new ExchangeRecord();
		record.setCostScore(exchange.getScore());
		record.setExchange(exchange);
		record.setUser(wechatMember.getUser());
		record.setMember(wechatMember);
		record.setName(exchange.getName());
		record.setUser(wechatMember.getUser());
		record.setEndTime(DateUtils.getDelayDate(exchange.getValidDays()));
		
		if(exchange.isAutoClear()){
			record.setStatus(ExchangeRecord.STATUS_CLEAR);
		}else{
			record.setStatus(ExchangeRecord.STATUS_NORMAL);
		}
		
		exchangeRecordService.save(record);
		  
		return record;
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-30  下午1:13:43
	 * @function <p> 积分扣除   </p>
	 * @param wechatMember
	 * @param exchange
	 */
	public static void addWechatMemberCostScore(WechatMember wechatMember, int cost, String remarks){
		
		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(-cost);			//操作的分数， 是负数 
		memberScore.setRemarks(remarks);
		
		memberScoreService.save(memberScore);
	}
	
	/**
	 * 购买成功通知
	 * @param userId
	 * @param openId
	 * @param first
	 * @param keyword1
	 * @param keyword2
	 * @param remark
	 * @param url
	 */
	public static void buySuccess(Long userId, String openId, String first, String keyword1,String keyword2, String remark,String url){
		UserMessageTemplate messageTemplate= userMessageTemplateService.getUserMessageTemplateByBid(userId, BusinessMessageTemplate.BUY_SUCCESS);
		if(messageTemplate!=null){
			String keyword3 = com.thinkgem.jeesite.common.utils.DateUtils.getDateTime();
			TemplateMessage templateMessage = TemplateMessageFactory.createBuySuccess(openId, messageTemplate.getTemplateId(), first, keyword1, keyword2, keyword3, remark, url);
					
			WeChat weChat=  weChatService.getWeChatByUserId(userId);
			String token = TokenUtils.getAPIToken(weChat);
			TemplateMessageUtils.sendTemplateMessage(token, templateMessage);
		}
	}
	
	 


	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		
		memberScoreService = (MemberScoreService)applicationContext.getBean("memberScoreService");
		couponConfigService = (CouponConfigService)applicationContext.getBean("couponConfigService");
		exchangeRecordService = (ExchangeRecordService)applicationContext.getBean("exchangeRecordService");
		wechatMemberService = (WechatMemberService)applicationContext.getBean("wechatMemberService");
		memberMoneyService = (MemberMoneyService)applicationContext.getBean("memberMoneyService");	
		userMessageTemplateService = (UserMessageTemplateService)applicationContext.getBean("userMessageTemplateService");
		weChatService = (WeChatService)applicationContext.getBean("weChatService");	
	}
}
